/**
 * 
 */
package com.cqcis.uip.base.framework.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 接口平台线程工厂
 * 
 * @author huangmy
 *
 */
public class LogServiceThreadFactory implements ThreadFactory {
	private final static AtomicInteger poolNumber = new AtomicInteger(1);
	private final static ThreadGroup group = new ThreadGroup("UIP-threadgroup");
	private final AtomicInteger threadNumber = new AtomicInteger(1);
	private final String namePrefix;

    public LogServiceThreadFactory() {        
        namePrefix = "pool-" + 
                      poolNumber.getAndIncrement() + 
                     "-thread-";
    }

    public Thread newThread(Runnable r) {
        Thread t = new LogThread(group, r, 
                              namePrefix + threadNumber.getAndIncrement(),
                              0);
        if (t.isDaemon())
            t.setDaemon(false);
        if (t.getPriority() != Thread.NORM_PRIORITY)
            t.setPriority(Thread.NORM_PRIORITY);
        return t;
    }
    
    public static class LogThread extends Thread {
    	private Map<String, Object> attributes = new ConcurrentHashMap<String, Object>();// 属性
    	private String id; // ID
    	
    	public LogThread(ThreadGroup group, Runnable target, String name,
                long stackSize) {
    		super(group, target, name, stackSize);
    	}
    	
    	public Map<String, Object> getAttributes() {
			return attributes;
		}

		public void setAttributes(Map<String, Object> attributes) {		
			this.attributes = attributes;
		}
	    	
		public Object setAttribute(String name, Object value) {		
			return this.attributes.put(name, value);
		}
		
		public Object getAttribute(String name) {
	        return this.attributes.get(name);
		}
		
		public Object removeAttribute(String name) {		
			return this.attributes.remove(name);
		}

		public String getLogThreadId() {
			return id;
		}

		public void setLogThreadId(String id) {
			this.id = id;
		}
    }
}
